package com.example.togethermove.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTabHost;
import androidx.viewpager.widget.ViewPager;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.example.togethermove.R;
import com.example.togethermove.base.BaseActivityone;
import com.example.togethermove.ui.fragment.CompetitonFragment;
import com.example.togethermove.ui.fragment.HonorFragment;
import com.example.togethermove.ui.fragment.MineFragment;
import com.example.togethermove.ui.fragment.SportmaseterFragment;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;




public class MainActivity extends BaseActivityone {


//    @BindView(R.id.ftb_main_bottom_layout)
//    FragmentTabHost mFtbMainBottomLayout;
//
//    @BindView(R.id.iv_main_click)
    ImageView mIvMainClick;
    private long exitTime = 0;  //back键按下时间
    private FragmentTabHost mTabHost;
    private ViewPager mViewPager;
    private List<Fragment> mFragmentList;
    private Class mClass[] = {SportmaseterFragment.class, HonorFragment.class, CompetitonFragment.class, MineFragment.class,};
    private Fragment mFragment[] = {new SportmaseterFragment(),new HonorFragment(),new CompetitonFragment(),new MineFragment()};
    private String mTitles[] = {"首页","荣誉","赛事","我的",};
    private int mImages[] = {
            R.drawable.selector_tab_mastersportman__,
            R.drawable.selector_tab_competition__,
            R.drawable.selector_tab_honor__,
            R.drawable.selector_tab_mine__,
    };





    public int getLayoutId() {
        return R.layout.activity_main;
    }


    protected void initViews(Bundle savedInstanceState) {
        mTabHost = findViewById(android.R.id.tabhost);
        mViewPager = findViewById(R.id.view_pager);
        mFragmentList = new ArrayList<>();
        mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) {
            mTabHost.getTabWidget().setDividerDrawable(null);
        }
        for (int i = 0; i < mFragment.length; i++) {
            TabHost.TabSpec tabSpec = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.DONUT) {
                tabSpec = mTabHost.newTabSpec(mTitles[i]).setIndicator(getTabView(i));
            }
            mTabHost.addTab(tabSpec, mClass[i], null);
            mFragmentList.add(mFragment[i]);
            mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.WHITE);
        }
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }


            public int getCount() {
                return mFragmentList.size();
            }
        });


    }





    protected void initData() {

    }



    protected void initListener() {
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                mViewPager.setCurrentItem(mTabHost.getCurrentTab());
            }
        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTabHost.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


//    @Override
//    public void onBackPressed() {
//        // 返回退出APP
//        if (!BackHandlerHelper.handleBackPress(this)) {
//            if ((System.currentTimeMillis() - exitTime) > 2000) {
//                Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_LONG).show();
//                exitTime = System.currentTimeMillis();
//            } else {
//                super.onBackPressed();
//            }
//        }
//    }

    private View getTabView(int index) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_item, null);
        ImageView image = view.findViewById(R.id.image);
        TextView title = view.findViewById(R.id.title);
        image.setImageResource(mImages[index]);
        title.setText(mTitles[index]);
        return view;
    }
//
//    @Override
//    public void onBackPressed() {
//        // 返回退出APP
//        if ((System.currentTimeMillis() - exitTime) > 2000) {
//            Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_LONG).show();
//            exitTime = System.currentTimeMillis();
//        } else {
//            super.onBackPressed();
//        }
//    }





}